package com.sk.bilibili.view.fragment.main;

import android.view.View;
import android.widget.RadioGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.main.HistoryAdapter;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.History;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.databinding.FragmentHistoryBinding;
import com.sk.bilibili.event.MainMenuEvent;
import com.sk.bilibili.model.fragment.main.HistoryModel;
import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;
import com.sk.bilibili.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class HistoryFragment extends BaseFragment<FragmentHistoryBinding, HistoryModel> implements
        RadioGroup.OnCheckedChangeListener, SwipeRefreshLayout.OnRefreshListener, OnBottomCallBack {

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
    }

    private HistoryAdapter historyAdapter;

    private void initView() {
        bind.srlShow.setColorSchemeResources(R.color.pink);
        bind.rvShow.setLayoutManager(new LinearLayoutManager(getContext()));
        historyAdapter = new HistoryAdapter(getContext(), this);
        bind.rvShow.setAdapter(historyAdapter);
    }

    private void bindEvent() {
        bind.ivMenu.setOnClickListener(v -> EventBus.getDefault().post(MainMenuEvent.closeEvent()));
        bind.rgShow.setOnCheckedChangeListener(this);
        bind.srlShow.setOnRefreshListener(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_all:model.setType(null);break;
            case R.id.rb_video:model.setType(0);break;
            case R.id.rb_live:model.setType(6);break;
        }
    }

    @Override
    public void onRefresh() {
        model.refresh();
    }

    @Override
    public void onBottom() {
        model.loadData();
    }

    public void refreshSuccess(List<History> records) {
        historyAdapter.setData(records);
        bind.srlShow.setRefreshing(false);
    }

    public void loadMoreSuccess(List<History> records) {
        if(records.isEmpty()){
            toast("没有更多了");
        }else {
            historyAdapter.addData(records);
        }
    }

    public void onLiveClick(Integer id) {
        toast("历史原因不支持回播");
    }

    public void onMemberClick(Integer id) {
        MemberActivity.start(getContext(),id);
    }

    public void onVideoSetClick(Integer id) {
        VideoSetActivity.start(getContext(),id);
    }

    public void onThemClick(Integer id) {
        ThemActivity.start(getContext(),id);
    }

    public void onFocusClick(Integer id) {
        model.switchFocus(id);
    }

    public void switchFocusSuccess(Focus focus) {
        toast(focus==null?"取消关注成功":"关注成功");
    }

}